package com.laicunba.ext.web.controller.console;

import java.util.List;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.service.RegistryService;

@Controller
@RequestMapping(value = "/console/ext")
public class ExtRegistryController extends BaseController {

	@Autowired
	private RegistryService registryService;

	@ResponseBody
	@RequestMapping(value = "/registry_list.htm")
	public String registryList() {
		List<RegistryDO> regitryList = registryService.list();
		return toJsonArray(regitryList);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/registry_delete.htm")
	public String registryDel(String id) {
		JSONObject json = new JSONObject();
		json.put("success", registryService.delete(RegistryDO.class, id));
		return json.toString();
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/registry_get.htm")
	public String registryGet(String id) {
		RegistryDO regist = registryService.get(RegistryDO.class, id);
		if (regist == null) {
			return "{}";
		}
		return toJsonObject(regist);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/registry_save.htm")
	public String registryCreate(String id, String value) {
		JSONObject json = new JSONObject(); 
		RegistryDO regist = registryService.get(RegistryDO.class, id);
		if (regist == null) {
			json.put("success", registryService.create(id, value)); 
		}else{ 
			json.put("success", registryService.update(id, value)); 
		} 
		return json.toJSONString();
	}

}
